package cn.lcfms.app.admin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.lcfms.app.admin.bean.ArticleBean;
import cn.lcfms.app.admin.service.CommonService;
import cn.lcfms.app.admin.service.DataService;
import cn.lcfms.bin.annotation.PermitPoll;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;
import cn.lcfms.bin.view.CommonEditView;
import cn.lcfms.bin.view.CommonPageView;

@PermitPoll(type="CMS管理",name="文章管理")
@Controller("admin.ArticleController")
@RequestMapping("/admin/article")
public class ArticleController extends AdminBaseController{
	
	@Autowired
	private CommonService c_service;
	
	@Autowired
	private DataService g_service;
	
	
	@RequestMapping("/list")
	public CommonPageView list(HttpServletRequest request){
		BaseService service=Application.getService();	
		service.setTable("categories");
		List<HashMap<String, Object>> list = service.column("id","title").where("pid<>0").selectList();
		StringBuffer text=new StringBuffer();
		StringBuffer values=new StringBuffer();
		for(int i=0;i<list.size();i++) {
			int id = (int) list.get(i).get("id");
			String title = (String) list.get(i).get("title");
			text.append(title);
			values.append(id);
			if(i!=list.size()-1) {
				text.append(",");
				values.append(",");
			}
		}
		service.setTable("articles","a");		
		service.leftJoin("categories","b").on("a.category_id=b.id");		
		service.column("a.id as 文章ID","a.title as 标题","b.title as 栏目","left(a.created_at,10) as 发布时间");	
		if(null!=request.getParameter("cid")){
			String cid=request.getParameter("cid");
			service.where("a.category_id="+cid);
		}
		service.order("a.id desc");
		CommonPageView view=new CommonPageView("文章管理",service);
		view.setToolBarUrl("添加内容", "icon-jia", "addArticle");
		view.setDeleteLayer("deleteArticle?id=${文章ID}");
		view.setUpdateUrl("editArticle?id=${文章ID}");
		
		view.setFilterInputLike("标题", "a.title", "");		
		view.setFilterSelectEq("栏目", "a.category_id", "全部,"+text.toString(), ","+values.toString(), "");
		view.setColumnWidth("标题", 600);
		return view;
	}
	
	@RequestMapping("listJson")
	public String listJson(HttpServletRequest request){
		BaseService service=Application.getService("articles");			
		service.column("id","name");
		String keywords=request.getParameter("keywords");
		if(null!=keywords){
			service.where("name like '%"+keywords+"%'");					
		}
		service.limit(0, 10);
		List<HashMap<String, Object>> list = service.selectList();
		JSONArray array=JSONArray.fromObject(list);
		return array.toString();
	}
	
	@RequestMapping("/deleteArticle")
	public String deleteArticle(int id){
		BaseService service=Application.getService("articles");
		service.deleteById(id);
		int row = service.affected_rows();
		return "{code:1,msg:删除成功"+row+"条记录！,url:list}";
	}
	
	@RequestMapping("/editArticle")
	public CommonEditView editArticle(int id){	
		BaseService service=Application.getService("mark");
		List<HashMap<String, Object>> list = service.column("title").selectList();
		CommonEditView view=new CommonEditView("修改内容");
		service=Application.getService("articles");
		HashMap<String, Object> map = service.setData(id).where("id=#{id}").selectMap();
		int category_id = (int) map.get("category_id");
		HashMap<Integer, String> category = g_service.getCategory(category_id);
		view.addObject("categoryId", category.keySet());
		view.setHiddenForm("id", id);
		view.setInputForm("标题", "title", map.get("title"),"unempty");	
		view.setViewForm("frame/categoriesEdit.jsp");
		view.setCheckboxForm("标签", "labels", c_service.parseListToString("title", list), c_service.parseListToString("title", list), map.get("labels"));
		JSONArray photo = g_service.getArticleFile(id);
		view.setUploadImageForm("图片","imgfile", photo, 0, 10);
		view.setTextareaForm("摘要", "desc", map.get("desc"), 300);
		view.setEditorForm("内容", "details", map.get("details"));		
		view.setFormAction("saveEdit");
		return view;
	}
	
	@RequestMapping("/addArticle")
	public CommonEditView addArticle(){	
		BaseService service=Application.getService("mark");
		List<HashMap<String, Object>> list = service.column("title").selectList();
		CommonEditView view=new CommonEditView("增加内容");
		view.setInputForm("标题", "title", "", "unempty");		
		view.setViewForm("frame/categories.jsp");
		view.setCheckboxForm("标签", "labels", c_service.parseListToString("title", list), c_service.parseListToString("title", list), "");	
		view.setUploadImageForm("图片","imgfile", null, 0, 10);
		view.setTextareaForm("摘要", "desc", "", 300);
		view.setEditorForm("内容", "details", "");		
		view.setFormAction("saveEdit");		
		return view;
	}
	
	@RequestMapping("/saveEdit")
	public String saveEdit(ArticleBean article,HttpServletRequest request){
		int[] is = article.getCategory_ids();
		int category_id=0;
		if(is!=null) {
			category_id=is[is.length-1];
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		g_service.setPhoto(request, article, "imgfile");
		article.setCreated_at(sdf.format(date));
		article.setCategory_id(category_id);
		g_service.saveArticle(article);
		return "{code:1,msg:操作成功！,url:list}";
	}
	
}
